//
//  SQCLLocationManager.m
//  TQY
//
//  Created by 甘世清 on 2024/10/6.
//

#import "SQCLLocationManager.h"

@interface SQCLLocationManager()<CLLocationManagerDelegate>
@property (nonatomic , strong) citySuccessBlock  block;
@end
static SQCLLocationManager * instance = nil;
static dispatch_once_t onceToken;
@implementation SQCLLocationManager
+(instancetype)sharedManager{
 
    dispatch_once(&onceToken, ^{
        instance = [[SQCLLocationManager alloc]init];
     
    });
    return instance;
}
-(instancetype)init{
    
    self = [super init];
    if (self) {
        self.delegate = self;
        self.desiredAccuracy = kCLLocationAccuracyBest;
        self.distanceFilter = 1;
        

       
    }
    return self;
}
-(void)SQ_Location:(citySuccessBlock)city{
    self.block = city;
    [[SQCLLocationManager sharedManager] SQ_Location];
}
-(void)SQ_Location{
    [[SQCLLocationManager sharedManager] requestWhenInUseAuthorization];
    if ([SQCLLocationManager sharedManager].authorizationStatus != kCLAuthorizationStatusDenied ){
        [[SQCLLocationManager sharedManager] startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
    
    if (locations.count<=0) {
        return;
    }
    [[SQCLLocationManager sharedManager] stopUpdatingLocation];
    CLLocation * location = locations.firstObject;
    CLLocationCoordinate2D loca = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
    
    CLLocationCoordinate2D loca2 = [JZLocationConverter wgs84ToGcj02:loca];
    UserModel * model = [UserModel share];
    if (model.lat == loca2.latitude&&model.lng == loca2.longitude) {
        return;
    }
    model.lat = loca2.latitude;
    model.lng = loca2.longitude;
    [UserModel updateLocalUserModel:model];
    CLGeocoder *gecoder = [[CLGeocoder alloc]init];
    WS(weakSelf);
    [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks.count > 0) {
            CLPlacemark * place = placemarks.firstObject;
            NSString *city = place.locality;
            if ([city isNotEmpty]) {
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:city];
            }else if([place.administrativeArea isNotEmpty]) {
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:place.administrativeArea];
            }else{
                [weakSelf SQ_upinfo:loca2.latitude lng:loca2.longitude city:@""];
            }
        }
    }];
}

-(void)SQ_upinfo:(float)lat lng:(float)lng city:(NSString*)city{
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%.7f",lat] forKey:@"lat"];
    [dic setObject:[NSString stringWithFormat:@"%.7f",lng] forKey:@"lng"];
    [dic setObject:city forKey:@"city"];
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [UserModel share];
        model.city = city;
        model.lat = lat;
        model.lng = lng;
        [UserModel updateLocalUserModel:model];
        if (weakSelf.block) {
            self.block(city);
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error{
    DTLog(@"报错了%@",error);
}
 
 
- (void)dealloc{
    instance = nil;
   
}
@end
